// pages/userSpace/collect/collect.js
const app = getApp()
const util = require("../../../utils/util")

Page({
  data: {
    arrCollect: [],
    arrAddTime: []
  },

  updateCollect: function () {
    console.log("update collect");
    let typeId = 0;
    util.myRequest({
      url: util.api.collectList,
      data: { typeId },
      success: res => {
        this.setData({
          arrCollect: res.data.data
        });
        this.updateAddTime();
      }
    })
  },

  updateAddTime: function () {
    let arr = [];
    let arrData = this.data.arrCollect;
    for (let index = 0; index < arrData.length; index++) {
      let element = arrData[index];
      let time = util.timestampToTime(element.add_time);
      arr.push(time)
    };
    this.setData({
      arrAddTime: arr
    })
  },

  deleteCurrentCollect: function(event) {
    let _this = this;
    wx.showModal({
      title: '确定移除该收藏吗?',
      content: '该商品将被移除',
      success (res) {
        if (res.confirm) {
          let vid = event.currentTarget.dataset.info.value_id;
          console.log(vid)
          util.myRequest({
            url: util.api.collectAddordelete,
            method: "post",
            data: {
              typeId: 0,
              valueId: vid
            },
            success: res =>{
              _this.updateCollect();
            }
          })
        } else if (res.cancel) {
        }
      }
    })

  },

  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.updateCollect()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  toDetail: function(e) {
    let gid = e.currentTarget.dataset.id;
    console.log(gid)
    wx.navigateTo({
      url: '/pages/goodsDetail/goodsDetail?id=' + gid
    })
  }
})